const constant = require('./constant');
const api = require('./api');
const storage = require('./storage');
class HttpRequest{
  constructor(url){
    this.url = url;
    this.header = {'Deviceid':'loveqing'};
    // token失效的请求队列
    this.qeueu = [];
    this.isToGetNewToken = true;
  }
  request(options){
      return new Promise((resolve,reject) => {
        this.header['Authorization'] = 'Bearer '+ wx.getStorageSync(storage.LOCAL_JWT_KEY);
        wx.request({
          url: this.url+options.url,
          method:options.method,
          data:options.data,
          header: this.header,
          success: res => {  // code:50005 token过期
            if(res.data.code == 50005 || res.data.code == 50000){
              wx.reLaunch({
                url: '/pages/login/login'
              })
            }else{
              resolve(res.data);
            }
          },
          fail: err => {
            reject()
          },
          complete:()=>{
          }
        })
      })
  }
}
module.exports = new HttpRequest(constant.BASEURL)
